package com.atguigu.juc.one;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class Date {
    private final  Lock lock = new ReentrantLock();
    public void a()  {
        //不见不散的锁
        //lock.lock();  没有返回值
        //过期不候的锁
        //第一个参数：等候时间
        //第二个参数： 时间单位
        //有返回值 ，boolean
        boolean tryLock = false;
        try {
            tryLock = lock.tryLock(1, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (tryLock) {
            try {
                System.out.println("规定时间内获取到锁");
             TimeUnit.SECONDS.sleep(1);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        } else {
            System.out.println("规定时间内没有获取到锁");
        }
    }
}
//测试过期不候，限时等待
public class tryLockDemo {

    public static void main(String[] args) throws InterruptedException {
        Date date = new Date();
        for (int i = 0; i <5 ; i++) {
            new Thread(()->{
                date.a();
            }).start();
        }

    }

}
